﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TA.DotNetUtils
{
	public static class _IEnumerable_Extensions
	{
		public static string GetJoinedStrings(this IEnumerable This, string delimiter, bool includeEmpties, Func<object, string> toString)
		{
			StringBuilder sb = new StringBuilder();
			bool first = true;
			foreach (object t in This)
			{
				string value = toString(t);

				if (includeEmpties || !string.IsNullOrEmpty(value))
				{
					if (!first) sb.Append(delimiter);
					first = false;

					sb.Append(value);
				}
			}
			return sb.ToString();
		}
		public static string GetJoinedStrings(this IEnumerable This, string delimiter, bool includeEmpties, string formatString, params Func<object, object>[] getters)
		{
			return This.GetJoinedStrings(delimiter, includeEmpties, delegate(object t)
			{
				ArrayList values = new ArrayList();
				foreach (Func<object, object> getter in getters)
				{
					values.Add(getter(t));
				}
				return string.Format(formatString, values.ToArray());
			});
		}
	}
}
